﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace TotalApplication.services
{
    public class FileOperateService
    {
        #region FileStream流解决问题
        /// <summary>
        /// 使用 FileStream 读取文件内容 【同时设置文件指针】
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="bufferSize">缓冲池大小</param>
        /// <param name="seek">指针设置模式</param>
        /// <param name="seekPtr">指针位移参数</param>
        /// <returns></returns>
        public static string ReadFileByFileStream(string filePath, int bufferSize, SeekOrigin seek, long seekPtr)
        {
            if (string.IsNullOrEmpty(filePath))
                return "文件路径不能为空";

            try
            {
                using (var fileStream = new System.IO.FileStream(filePath, FileMode.Open, System.IO.FileAccess.Read))
                {
                    byte[] buffer = new byte[bufferSize];
                    char[] chars = new char[bufferSize];

                    fileStream.Seek(seekPtr, seek);

                    // Read bytes from the file into the buffer & record how many bytes were read
                    int bytesRead = fileStream.Read(buffer, 0, buffer.Length);

                    // Convert the bytes to a string using UTF8 encoding
                    string content = Encoding.UTF8.GetString(buffer, 0, bytesRead);

                    // return the content
                    return content;
                }
            }
            catch (Exception ex)
            {
                return $"错误: {ex.Message}";
            }

        }

        /// <summary>
        /// 使用 FileStream 读取文件内容 
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="bufferSize">缓冲池大小</param>
        /// <returns></returns>
        public static string ReadFileByFileStream(string filePath, int bufferSize)
        {
            if (string.IsNullOrEmpty(filePath))
                return "文件路径不能为空";

            try
            {
                using (var fileStream = new System.IO.FileStream(filePath, FileMode.Open, System.IO.FileAccess.Read))
                {
                    byte[] buffer = new byte[bufferSize];
                    char[] chars = new char[bufferSize];

                    // Read bytes from the file into the buffer & record how many bytes were read
                    int bytesRead = fileStream.Read(buffer, 0, buffer.Length);

                    // Convert the bytes to a string using UTF8 encoding
                    string content = Encoding.UTF8.GetString(buffer, 0, bytesRead);

                    // return the content
                    return content;
                }
            }
            catch (Exception ex)
            {
                return $"错误: {ex.Message}";
            }

        }

        /// <summary>
        /// 使用 FileStream 写入文件内容 
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="content">写入的内容字符串</param>
        /// <returns></returns>
        public static void WriteFileByFileStream(string filePath, string content, FileMode mode)
        {
            if (string.IsNullOrEmpty(filePath))
                MessageBox.Show("文件路径不能为空");

            try
            {
                using (var fileStream = new System.IO.FileStream(filePath, mode, System.IO.FileAccess.Write))
                {
                    // Convert the string content to bytes using UTF8 encoding
                    // Note: Encoding.UTF8.GetBytes() is more straightforward than using an Encoder

                    //Encoder utf8Encoder = Encoding.UTF8.GetEncoder();
                    //var contentBytes = new byte[utf8Encoder.GetByteCount(content.ToCharArray(), 0, content.Length, true)];
                    byte[] contentBytes = Encoding.UTF8.GetBytes(content);


                    // Write the bytes to the file
                    fileStream.Write(contentBytes, 0, contentBytes.Length);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误: {ex.Message}");
            }

        }

        #endregion



    }
}
